<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="all" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, index) in goodList" :key="index">
          <td>
            <input
              type="checkbox"
              name=""
              id=""
              :checked="item.checked"
              @change="$emit('changes', index)"
            />
          </td>
          <td>{{ item.name }}</td>
          <td>{{ item.price }}</td>
          <td>
            <button @click="item.num++">+</button>
            {{ item.num }}
            <button @click="item.num--" :disabled="item.num === 0">-</button>
          </td>
          <td>{{ item.price * item.num }}</td>
          <button @click="$emit('remove', index)">删除</button>
        </tr>
      </thead>
      <tbody></tbody>
      <tfoot>
        <tr>
          <td>合计:{{ price }}</td>
          <td colspan="5"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  props: {
    goodList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {};
  },
  methods: {
    arr(e) {
      e.target.checked
        ? this.$emit("checkadd", false)
        : this.$emit("checkadd", true);
    },
  },
  computed: {
    price() {
      const arr = this.goodList.filter((item) => item.checked === true);
      return arr.reduce((sum, item) => sum + item.price * item.num, 0);
    },
    all: {
      get() {
        return this.goodList.every((item) => item.checked === true);
      },
      set(value) {
        this.$emit("checkadd", value);
      },
    },
  },
};
</script>

<style></style>
